﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using myMvcBlog.Models;

namespace myMvcBlog.Areas.Admin.Controllers
{
    public class CommentController : AdminBaseController
    {
        [Authorize]
        public ActionResult List(int? page)
        {
            var cmts = Comment.All().OrderByDescending(c => c.CommentTime);

            ListView<Comment> lvComments = new ListView<Comment>();
            lvComments.List = cmts.Skip((page ?? 0) * ItemsPerPage).Take(ItemsPerPage).ToList();

            PagerView pager = new PagerView();
            pager.PageCount = (int)Math.Ceiling((double)cmts.Count() / ItemsPerPage);
            pager.CurrentPage = page ?? 0;
            pager.ActionName = "List";
            pager.ControllerName = "Comment";

            lvComments.Pager = pager;

            return View(lvComments);
        }

        [Authorize]
        public ActionResult Delete(long id)
        {
            var comment = Comment.SingleOrDefault(c => c.ID == id);
            comment.Delete();

            return RedirectToAction("List");
        }

    }
}
